﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;
using System.Management;
using SpellPyWbCls;

namespace TLCApp.pulic
{
    public abstract class Cls_Func
    {
      
        //得到汉字的拼音码
        public static string GetPyCode(string mStr)
        {
            char w;
            string s;
            string Py;
            int i = 1;
            DataTable mDt;
            int UC;
            s = mStr;
            Py = "";
            mDt = mTable();
            while ((s.Length > 0))
            {
                w =char.Parse( mStr.Substring((i - 1), 1));
                s = mStr.Substring((mStr.Length - (s.Length - 1)));
                UC = w;
                if (((UC >= 19968)&& (UC <= 40869)))
                {
                    DataRow[] m;
                    m = mDt.Select(("Fword>=\'" + (w + "\'")), "FPy");
                    Py = (Py + m[0][0].ToString());
                }
                else
                {
                    Py = (Py + w.ToString().ToUpper());
                }
                i = (i + 1);
            }
            return Py;
        }

        private static DataTable mTable()
        {
            DataTable mDt = new DataTable();
            DataColumn mCol1 = new DataColumn();
            DataColumn mCol2 = new DataColumn();
            string[] mArray = new string[23];
            DataRow myRow;
            mCol1.ColumnName = "FPy";
            mCol2.ColumnName = "FWord";
            mDt.Columns.Add(mCol1);
            mDt.Columns.Add(mCol2);

            mArray[0] = "A驁"; mArray[1] = "B簿"; mArray[2] = "C錯"; mArray[3] = "D鵽";
            mArray[4] = "E樲" ; mArray[5] = "F鰒" ; mArray[6] = "G腂" ; mArray[7] = "H夻";
            mArray[8] = "J攈" ; mArray[9] = "K穒" ; mArray[10] = "L鱳" ; mArray[11] = "M旀";
            mArray[12] = "N桛" ; mArray[13] = "O漚" ; mArray[14] = "P曝" ; mArray[15] = "Q囕";
            mArray[16] = "R鶸" ; mArray[17] = "S蜶" ; mArray[18] = "T籜" ; mArray[19] = "W鶩";
            mArray[20] = "X鑂" ; mArray[21] = "Y韻" ; mArray[22] = "Z咗";

            int i;
            for (i = 0; (i<= (mArray.Length - 1)); i++)
            {
                myRow = mDt.NewRow();
                myRow[0] = mArray[i].Substring(0, 1);
                myRow[1] = mArray[i].Substring(1, 1);
                mDt.Rows.Add(myRow);
            }
            return mDt;
        }
        /// <summary>
        /// 获取拼音码
        /// </summary>
        /// <param name="mStr"></param>
        /// <param name="?"></param>
        /// <returns></returns>
        public static string GetPym(string mStr)
        {
            SpellPyWbCls.SpellAndWbCode swCls = new SpellAndWbCode();
            return swCls.GetSpellCode(mStr);


        }
        /// <summary>
        /// 拼音码
        /// </summary>
        /// <param name="mStr"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static string GetPym( string mStr,int result)
        { 
                  SpellPyWbCls.SpellAndWbCode swCls = new SpellAndWbCode();                       
                return  swCls.GetSpellCode(mStr, result);

        
         }
         /// <summary>
        /// 五笔码
        /// </summary>
        /// <param name="mStr"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static string GetWbm(string mStr)
        {
            SpellPyWbCls.SpellAndWbCode swCls = new SpellAndWbCode();
            return swCls.GetWBCode(mStr);


        }
           /// <summary>
        /// 五笔码
        /// </summary>
        /// <param name="mStr"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static string GetWbm(string mStr, int result)
        {
            SpellPyWbCls.SpellAndWbCode swCls = new SpellAndWbCode();
            return swCls.GetWBCode(mStr, result);


        }

        /// <summary>
        /// 获取终端IP地址
        /// </summary>
        /// <returns>返回IP地址</returns>
        public string GetIPAddress()
        {
            string str="";
            try
            {
                //ManagementObjectCollection instances = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
                //foreach (ManagementObject obj2 in instances)
                //{
                //    if (Convert.ToBoolean(obj2["IPEnabled"]))
                //    {
                //        Array array = (Array)obj2.Properties["IpAddress"].Value;
                //        str = array.GetValue(0).ToString();
                //        break;
                //    }
                //}
                //instances = null;                
            }
            catch
            {                
            }
            return str;
        }




        /// <summary>
        /// 获取终端MAC地址
        /// </summary>
        /// <returns>返回MAC地址</returns>
        public static string GetMacAddress()
        {
            try
            {
               string str = "";

                //ManagementObjectCollection instances = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
                //foreach (ManagementObject obj2 in instances)
                //{
                //    if (Convert.ToBoolean(obj2["IPEnabled"]))
                //    {
                //        str = obj2["MacAddress"].ToString();
                //        break;
                //    }
                //}
                //instances = null;
                //str = str.Replace(":", "-");
                return str;
            }
            catch
            {
                return "HT-BB-CC-DD-EE-FF";
                //throw exception1;
            }
        }



        /// <summary>
        /// 限定为只能输入数字
        /// </summary>
        /// <param name="ect">控件的ketpress事件</param>
        /// <param name="bl">是否带有小数点</param>
        /// <returns></returns>
        public static bool NumberMust(KeyPressEventArgs ect, bool bl)
        {
            ect.Handled = ect.KeyChar < '0' || ect.KeyChar > '9';
            if (ect.KeyChar == ((char)8))
            {
                ect.Handled = false;
            }
            if (bl == false)
            {
                if (ect.KeyChar == '.')
                {
                    ect.Handled = false;
                }
            }

            return ect.Handled;
        }

        /// <summary>
        /// 限定为只能输入数字 英文字符 下划线
        /// hwadley 20110111
        /// </summary>
        /// <param name="ect">控件的ketpress事件</param>
        /// <param name="bl">是否带有小数点</param>
        /// <returns></returns>
        public static bool CharOrNumberMust(KeyPressEventArgs ect)
        {
            if ((ect.KeyChar >= '0' && ect.KeyChar <= '9') || (ect.KeyChar >= 'A' && ect.KeyChar <= 'Z') ||
                         (ect.KeyChar >= 'a' && ect.KeyChar <= 'z') || (ect.KeyChar == 8) || (ect.KeyChar == '_'))
            {
                ect.Handled = false;
            }
            else if (char.IsControl(ect.KeyChar))
            {
                ect.Handled = false;
            }
            else
            {

                ect.Handled = true;
            }
            return ect.Handled;
        }

        /// <summary>
        /// 导出DataGridView数据到Excel
        /// </summary>
        /// <param name="dgv">需要导出的DataGridView</param>
        public static void DataGridToExcel(DataGridView dgv)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();//创建Excel表
                myExcel.Visible = true;
                Microsoft.Office.Interop.Excel.Workbooks myWorkbooks = myExcel.Workbooks;//创建Excel工作表
                Microsoft.Office.Interop.Excel.Workbook myWorkbook = myWorkbooks.Add(System.Reflection.Missing.Value); //创建Excel工作表
                Microsoft.Office.Interop.Excel.Worksheet myWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkbook.Worksheets[1];//创建Excel工作表Sheet1页                

                int tc = dgv.Columns.Count;

                int cl = 0;
                int cc = 0;
                object[] myhead = new object[dgv.Columns.Count];
                for (cc = 0; cc < dgv.Columns.Count; cc++)
                {
                    if (dgv.Columns[cc].Visible == true)
                    {
                        myhead[cl] = dgv.Columns[cc].HeaderText.ToString();
                        cl++;
                    }
                }

                Microsoft.Office.Interop.Excel.Range myrange = myWorksheet.get_Range("A1", System.Reflection.Missing.Value);//选择页的范围
                //myrange.Value2 = "是打算打算打";
                //myrange.Merge(true);

                myrange = myWorksheet.get_Range("A1", System.Reflection.Missing.Value);

                myrange = myrange.get_Resize(1, cl);
                myrange.Value2 = myhead;    //表头的值传入Excel表
                cc = 0;
                if (dgv.Rows.Count > 0)
                {
                    myrange = myWorksheet.get_Range("A2", System.Reflection.Missing.Value);
                    int row = 0;
                    row = dgv.Rows.Count;   //设置表的行数
                    int col = dgv.Columns.Count;  //设置表的列数
                    object[,] mydata = new object[row, col];
                    for (int i = 0; i < row; i++)   //循环读取DataGridView上的数据
                    {
                        for (int j = 0; j < col; j++)
                        {
                            if (dgv.Columns[j].Visible == true)
                            {
                                mydata[i, cc] = dgv[j, i].Value.ToString();//读取DataGridView上的值保存到mydata数组中，这里要注意哦，DataGridView是列在前，而Excel刚好相反
                                cc++;
                            }
                        }
                        cc = 0;
                    }
                    myrange = myrange.get_Resize(row, col);
                    myrange.Value2 = mydata;//把madata数组的值存到Excel表导出
                    myrange.EntireColumn.AutoFit();
                }
                myExcel = null;//清空表单
            }
            catch (Exception ex)
            {
                //C_PubInfo cpi = new C_PubInfo();
                Cls_Popup.MsgErrBox(ex.Message);
            }
        }

        /// <summary>
        /// 验证是 0非法 1汉字 2字母 3数字 4非负浮点数,5正整数
        /// </summary>
        /// <param name="str">验证字符串</param>
        /// <returns></returns>
        public static int CheckString(string str)
        {
            if (Regex.IsMatch(str, @"^[\u4e00-\u9fa5]+$")) return 1;
            if (Regex.IsMatch(str, "^[a-zA-Z]+$")) return 2;
            if (Regex.IsMatch(str, "^\\d+(\\.\\d+)?$")) return 5;
            if (Regex.IsMatch(str, "^[0-9]+$")) return 3;
            if (Regex.IsMatch(str, @"^\d+(\.\d+)?$")) return 4;

            return 0;
        }
        /// <summary>
        /// add by Thomas
        /// 验证是 0非法 1汉字 2字母 3整数 4非负浮点数,5正整数
        /// </summary>
        /// <param name="str">验证字符串</param>
        /// <returns></returns>
        public static bool CheckString(string str, string strType)
        {
            bool flag = false;
            switch (strType)
            {
                case "1":

                    flag = Regex.IsMatch(str, @"^[\u4e00-\u9fa5]+$");
                    break;
                case "2":

                    flag = Regex.IsMatch(str, "^[a-zA-Z]+$");
                    break;

                case "3":

                    flag = Regex.IsMatch(str, "^[0-9]+$");
                    break;

                case "4":

                    flag = Regex.IsMatch(str, @"^\d+(\.\d+)?$");
                    break;

                case "5":

                    flag = Regex.IsMatch(str, "^\\d+(\\.\\d+)?$");
                    break;

            }
            return flag;

        }
        #region
        /// <summary>
        /// 获得年龄
        /// </summary>
        /// <param name="tAge"> 出生日期</param>
        /// <returns></returns>
        public static int GetAge(DateTime tAge)
        {
            int iAge = 0;
            iAge = DateTime.Now.Year - tAge.Year;

            if (DateTime.Now.Month == tAge.Month)
            {

                if (DateTime.Now.Date == tAge.Date)
                {
                    iAge = iAge - 1;
                }
                else
                {
                    iAge = iAge;

                }
            }
            else
            {
                if (DateTime.Now.Month > tAge.Month)
                {
                    iAge = iAge;
                }

                else
                {

                    iAge = iAge - 1;

                }

            }
            if (iAge < 0)
            {
                iAge = 0;
            }
            return iAge;

        }
        #endregion
        #region //获取转化系数
        /// <summary>
        /// 获取转化系数返回结果一天为单位 
        /// </summary>
        /// <param name="dbCounts">频次次数</param>
        /// <param name="ditemCount">频次数目</param>
        /// <param name="Unit" >单位  H,D,W,T</param>
        /// <returns></returns>
        public static double GetRate(double dbCounts, double ditemCount, string Unit)
        {
            double dResult = 1;
            try
            {
                switch (Unit.ToUpper())
                {

                    case "H"://小时
                        dResult = (dbCounts * 24) / ditemCount;
                        break;
                    case "D"://天
                        dResult = (dbCounts * 1) / ditemCount;
                        break;
                    case "W"://
                        dResult = dbCounts / (ditemCount * 7);
                        break;
                    case "T":
                        break;
                }
                return dResult;

            }
            catch (Exception ex)
            {
                TLCApp.pulic.WriteLog.WriteLoginf(ex.ToString());
                return dResult;
            }


        }
        #endregion
        /// <summary>
        /// s删除一类文件
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public static bool DeleFiles(string strPath, string Type)
        {
            try
            {
                DirectoryInfo fold = new DirectoryInfo(@strPath);
                if (fold.Exists)
                {
                    FileInfo[] files = fold.GetFiles("*." + Type + "");
                    foreach (FileInfo f in files)//删除目录下*.txt
                    {
                        f.Delete();
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool DeleFiles(string strPath)
        {
            try
            {

                FileInfo file = new FileInfo(@strPath);
                if (file.Exists)
                {
                    file.Delete(); //删除单个文件
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #region    两个结构不同的DataTable合并
        /// <summary>
        /// 将两个列不同的DataTable合并成一个新的DataTable
        /// hwadley
        /// </summary>
        /// <param name="dt1">表1</param>
        /// <param name="dt2">表2</param>
        /// <param name="DTName">合并后新的表名</param>
        /// <returns></returns>
        public static DataTable UniteDataTable(DataTable dt1, DataTable dt2, string DTName)
        {
            DataTable dt3 = dt1.Clone();
            for (int i = 0; i < dt2.Columns.Count; i++)
            {
                dt3.Columns.Add(dt2.Columns[i].ColumnName);
            }
            object[] obj = new object[dt3.Columns.Count];

            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                dt1.Rows[i].ItemArray.CopyTo(obj, 0);
                dt3.Rows.Add(obj);
            }

            if (dt1.Rows.Count >= dt2.Rows.Count)
            {
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    for (int j = 0; j < dt2.Columns.Count; j++)
                    {
                        dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();
                    }
                }
            }
            else
            {
                DataRow dr3;
                for (int i = 0; i < dt2.Rows.Count - dt1.Rows.Count; i++)
                {
                    dr3 = dt3.NewRow();
                    dt3.Rows.Add(dr3);
                }
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    for (int j = 0; j < dt2.Columns.Count; j++)
                    {
                        dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();
                    }
                }
            }
            dt3.TableName = DTName;  //设置DT的名字
            return dt3;
        }
        #endregion

        #region
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Start">开始时间</param>
        /// <param name="End"></param>
        /// <returns></returns>
        public static bool isTrueTime(string Start, string End)
        {

            bool result = false;
            try
            {
                if (Convert.ToDateTime(End) >= Convert.ToDateTime(Start))
                {
                    result = true;
                }
                else
                {
                    TLCApp.pulic.Cls_Popup.MsgWarnBox("开始时间不可以大于结束时间");
                }
                return result;
            }
            catch (Exception ex)
            {
                TLCApp.pulic.Cls_Popup.MsgErrBox(ex.ToString());
                return result;
            }
        }
        #endregion
        
      ///<summary>
     /// 门诊号标准化
     /// </summary>
     /// <param name="MZH"></param>
     /// <returns></returns>
     /// <remarks></remarks>
   public static string  FormatMZH(string  MZH, Int16 ilen ) 
    {  string  sTempMZH = MZH;
        if(sTempMZH !="") 
        {
            
                 string  Tmp =sTempMZH;

                 sTempMZH = Tmp.PadLeft(ilen, Convert.ToChar("0"));
             
        
        }
        
      return sTempMZH;
  
    }

       public static DataTable GetBaseData(string strcodetype)
       {
           DataTable dt = null;
           try
           {
               dt = TLCApp.pulic.UserIfo.dsBasedata.Copy().Tables[0];
               DataView dv = dt.DefaultView;
               dv.RowFilter = string.Format("CodeType='{0}'", strcodetype);
               dt=dv.ToTable();
               return dt;

           }
           catch (Exception ex)
           {
               throw ex;
           }

   
       }


       public static string filetrchar(string strstr)
       {
           string reslut = strstr.Replace('<', ' ').Replace('>', ' ').Replace('/', ' ').Replace('\\', ' ');

           return reslut;
       }
    }
}
